package com.apes.auto.autoparts.vin;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.auto.autoparts.repository.AutopartsDao;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.party.model.PartyRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName EPCServic
 * @Description TODO
 * @Author pengyb
 * @Date 2019/8/23 14:19
 * @Version 1.0
 **/
@Service("epcService")
public class EPCService extends DomainService {

    @Autowired
    private AutopartsDao autopartsDao;

    //树形展示
    public List<Map> treeSearchEpcCatalog(SimpleRequest request) {
        //手动解析
        JSONObject fieldMap = new JSONObject();
        List<Map> fields = request.get("condition");
        fields.stream().forEach(field -> {
            if (field.get("field").toString().equals("EPC_BBMC")) {
                fieldMap.put("EPC_BBMC", field.get("value").toString());
            }
            if (field.get("field").toString().equals("MY_MODEL_STD_ID")) {
                fieldMap.put("MY_MODEL_STD_ID", field.get("value").toString());
            }
        });
        List<Map> epc = autopartsDao.queryEPC_STD_CATA(fieldMap);
        epc.stream().forEach(e -> {
            String path = e.get("CATA_INDEX").toString();
            e.put("path", path.replaceAll("_", "/"));
            e.put("id", e.get("CATA_ID"));
            e.put("name", e.get("NAME_ZH"));
        });
        return epc;

    }

    /**
     * 树形节点查询
     *
     * @param request
     * @return
     */
    public JSONObject treeSearchNodeEpcCatalog(SimpleRequest request) {
        //手动主从结构
        JSONObject fieldMap = new JSONObject();
        fieldMap.put("CATA_ID", request.get("id"));
        List<Map> stdCata = autopartsDao.queryEPC_STD_CATA(fieldMap);  //主表
        if (stdCata.equals(null))
            throw new RuntimeException("目录" + request.get("id") + "已经被删除或改变");
        if (stdCata.size() > 1)
            throw new RuntimeException("目录" + request.get("id") + "查询出多条记录，数据出错！");
        JSONObject result = (JSONObject) JSONObject.toJSON(stdCata.get(0));
        result.put("cataDzb", autopartsDao.queryEPC_STD_CATA_DZB(result));   //从表
        return result;
    }

    /**
     * 查询最大当前目录id
     *
     * @param request
     * @return
     */
    public int queryCurrentId(SimpleRequest request) {
        return autopartsDao.queryMaxCurrentId(request.getJO());
    }

    /**
     * 新增
     *
     * @param request
     * @return
     */
    public String saveEpcCatalog(SimpleRequest request) {
        JSONObject requestJO = request.getJO();
        //从表
        JSONArray cataDzb = request.get("cataDzb");
        cataDzb.forEach(c -> {
            requestJO.put("PLID", ((JSONObject) c).get("PLID"));
            autopartsDao.insertEPC_STD_CATA_DZB(requestJO);
        });
        //主表
        autopartsDao.insertEPC_STD_CATA(requestJO);
        return "succeed";
    }

    /**
     * 修改
     *
     * @param request
     * @return
     */
    public String updateEpcCatalog(SimpleRequest request) {
        JSONObject requestJO = request.getJO();
        //从表
        JSONArray cataDzb = request.get("cataDzb");
        //没有主键 没有唯一条件  一对多 情况下无法执行更新操作 选择先清空 在添加
        autopartsDao.deleteEPC_STD_CATA_DZB(requestJO);
        cataDzb.forEach(c -> {
            requestJO.put("PLID", ((JSONObject) c).get("PLID"));
            autopartsDao.insertEPC_STD_CATA_DZB(requestJO);
        });
        //主表
        autopartsDao.updateEPC_STD_CATA(requestJO);
        return "succeed";
    }

    /**
     * 删除
     *
     * @param request
     * @return
     */
    public String deleteEpcCatalog(SimpleRequest request) {
        JSONObject requestJO = request.getJO();
        autopartsDao.deleteEPC_STD_CATA(requestJO);
        return "succeed";
    }

    /**
     * 保存EPC版本
     *
     * @param request
     * @return
     */
    public String saveEpcOeVersions(SimpleRequest request) {
        JSONObject requestJO = request.getJO();
        //从表
        JSONArray oepjmc = request.get("OEPJMC");
        oepjmc.forEach(o -> {
            JSONObject object = (JSONObject) o;
            requestJO.put("EPC_BBMC", object.get("EPC_BBMC"));
            object.put("LRR", ((PartyRole) getPerson(request)).getName());
            object.put("TS01", DateUtil.format(new Date(), "yyyyMMdd.hhmmss.SSS"));
            autopartsDao.insertEPC_OE_PJMC(object);
        });
        //主表
        autopartsDao.insertEPC_OE_V(requestJO);
        return "succeed";
    }

    public String updateEpcOeVersions(SimpleRequest request) {
        JSONObject requestJO = request.getJO();
        //从表
        JSONArray oepjmc = request.get("OEPJMC");
        autopartsDao.deleteEPC_OE_PJMC(oepjmc.getJSONObject(0));
        oepjmc.forEach(o -> {
            JSONObject object = (JSONObject) o;
            requestJO.put("EPC_BBMC", object.get("EPC_BBMC"));
            autopartsDao.insertEPC_OE_PJMC(object);
        });

        autopartsDao.updateEPC_V(requestJO);
        return "succeed";
    }


    /**
     * 查询
     *
     * @param request
     * @return
     */
    public Page<Map> searchEpcOeVersions(SimpleRequest request) {
        //手动解析
        JSONObject fieldMap = new JSONObject();
        List<Map> fields = request.get("condition");
        fields.stream().forEach(field -> {
            if (field.get("field").toString().equals("EPC_BBMC")) {
                fieldMap.put("EPC_BBMC", field.get("value").toString());
            }
            if (field.get("field").toString().equals("MY_MODEL_STD_ID")) {
                fieldMap.put("MY_MODEL_STD_ID", field.get("value").toString());
            }
            if (field.get("field").toString().equals("PARTNUMBER")) {
                fieldMap.put("PARTNUMBER", field.get("value").toString());
            }
        });
        List<Map> oeV = autopartsDao.searchEPC_OE_V(fieldMap);

        oeV.forEach(v -> {
            v.put("EPC_BBMC",fieldMap.get("EPC_BBMC"));
                    v.put("OEPJMC", autopartsDao.searchEPC_OE_PJMC(new JSONObject(v)));
                }
        );
        Pageable pageable = PageRequest.of(request.get("page"), request.get("size"));
        PageImpl pPage = new PageImpl<>(oeV, pageable, oeV.size());
        return pPage;

    }
}
